<?php
/**
 * @version $Id: form_video.php 46 2011-10-19 09:05:33Z truongnhan0311@gmail.com $
 */

define('_JEXEC', 1);
// No direct access.
defined('_JEXEC') or die;
// no direct access
defined('_JEXEC') or die('Restricted access');

define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', dirname(__FILE__).DS.'..'.DS.'..'.DS.'..'.DS.'..'.DS);

require_once (JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once (JPATH_BASE .DS.'includes'.DS.'framework.php' );

jimport('joomla.filesystem.file');
jimport('joomla.database.database');
jimport('joomla.database.table');

$mainframe = &JFactory::getApplication('site');
$mainframe->initialise();

if (!empty($_FILES)) {    
    $file = $_FILES['Filedata'];
    $typeupload = $_POST['typeupload'];
    $userid = $_POST['userid'];
    $file_ext = $_REQUEST['fileext'];
    
    switch ($typeupload) {
        case 'image':
            upload($file,$typeupload,'img_',$file_ext,$userid,1);
            break;
        case 'transaction':
            upload($file,$typeupload,'tran_',$file_ext,$userid,0);
            break;
		case 'logo':
			upload($file,$typeupload,'logo_',$file_ext,$userid,1);
			break;
        default:
            upload($file,$typeupload,'pro_',$file_ext,$userid,1);
            break;
    }
}

    function upload($file,$type,$name,$file_ext,$userid,$thumb = 0){
        $path = JPATH_SITE.DS.'wrd'.DS.$type;
        $filename = strtolower(JFile::makeSafe($file['name']));
        $ext = strtolower(JFile::getExt($filename));
        $caption = str_ireplace('.'.$ext, '', $file['name']);
        
        $file_ext = str_ireplace('*.', '', $file_ext);
        $arr_ext = explode(';', $file_ext);
			
        if(in_array($ext, $arr_ext)){
            $newfile = $name.rand(0,time()).time().'.'.$ext;
            
            $src = $file['tmp_name'];
            $dest = $path. DS . $newfile;
				
			if(JFile::upload($src, $dest)){
				
				//store into database
				$obj = new stdClass();
				$obj->id = null;
				$obj->name = $newfile;
				$obj->type = $type;
				$obj->product_id = 0;
				$obj->user_id = $userid;
				$obj->caption = $caption;
				$obj->dateadd = date("Y-m-d H:i:s");
				$db = JFactory::getDBO();
				
				$db->insertObject('#__wrd_file', $obj);
				$id = $db->insertid();
				
				if($thumb == 1)
					createThumbs($dest,$ext,$newfile,$type);
				
				$result['fileid'] = $id;
				$result['filename'] = $newfile;
				echo json_encode($result);
			}
			else{
				echo 'can not upload';
			}
						
        }
        jexit();
    }

    function createThumbs($image, $ex,$thumb,$type='' ) {
        $path = JPATH_SITE.DS.'wrd'.DS.'thumb';
        
        $pathToImages = $image;
        $pathToThumbs = $path.DS.$thumb;
        
        // load image and get image size
          if($ex  == 'jpg')
              $img = imagecreatefromjpeg( "{$pathToImages}" );
          else if($ex  == 'png')
            $img =imagecreatefrompng ("{$pathToImages}");
          
          $width = imagesx( $img );
          $height = imagesy( $img );
        if($type != 'image' && $type != 'transaction'){
            $new_width = floor(100);
              $new_height = floor(100);
        }else{
              if($width <= 160){
                  // calculate thumbnail size
                  $new_width = floor($width);
                  $new_height = floor($height);
              }
              else{
                  // calculate thumbnail size
                  $new_width = floor(160);
                  $new_height = floor( $height/$width * 160);
              }
        }

          // create a new temporary image
          $tmp_img = imagecreatetruecolor( $new_width, $new_height );

        // copy and resize old image into new image 
           imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
       
          // save thumbnail into a file
          if($ex  == 'jpg')
              imagejpeg( $tmp_img, "{$pathToThumbs}" );
          else if($ex  == 'png')
            imagepng ($tmp_img, "{$pathToThumbs}");
        
        return $thumb;
    }